<template>
  <div class="container">
    <div class="user_list">
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="search">
          <a-form-item label="日期选择">
            <a-range-picker
              v-model="queryParam.time"
              :ranges="{
              '1周': [moment().startOf('day').subtract(1, 'weeks'), moment()],
              半个月: [moment().startOf('day').subtract(14, 'days'), moment()],
              '3个月': [moment().startOf('day').subtract(3, 'months'), moment()],
              '6个月': [moment().startOf('day').subtract(6, 'months'), moment()],
              '1年': [moment().startOf('day').subtract(1, 'years'), moment()],
            }"
              format="YYYY-MM-DD"
              @change="onChangeData"
              :disabled-date="disabled"
            />
          </a-form-item>
          <a-form-item label="用户ID">
            <a-input v-model="queryParam.userNo" placeholder="用户ID" />
          </a-form-item>
          <a-form-item label="登录名">
            <a-input v-model="queryParam.userPhone" placeholder="登录名" />
          </a-form-item>
          <a-form-item label="实名状态">
            <a-select v-model="queryParam.certificationState" placeholder="请选择实名状态" style="width: 200px">
              <a-select-option :key="item.key" v-for="item in option" @onchange="onchange(item)">
                {{ item.label }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="状态">
            <a-select v-model="queryParam.userState" placeholder="请选择状态" style="width: 200px">
              <a-select-option :key="item.key" v-for="item in option1" @onchange="onchange(item)">
                {{ item.label }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item>
            <a-button type="primary" htmlType="submit" @click="search">查询</a-button>
            <a-button htmlType="submit" class="ml15" @click="searchReset">重置</a-button>
          </a-form-item>
        </a-form>
      </div>
      <s-table
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        bordered
        :rowKey="(record) => record.id"
      >
        <div slot="avatar" slot-scope="text">
          <img :src="require('@/assets/images/header.png')" alt="" class="avatar" v-if="!text" />
          <img :src="text" alt="" class="avatar" v-else />
        </div>
        <div slot="action" slot-scope="text, record">
          <p>
            <a @click="view(record, 0)">查看用户详情</a>
          </p>
          <p>
            <a @click="view(record, 1)">重置登录密码</a>
          </p>
          <p>
            <a @click="view(record, 2)">重置支付密码</a>
          </p>
          <p>
            <a @click="view(record, 3)" :disabled="record.userState === 1">
              {{ record.userState === 2 ? '解冻' : '冻结' }}账号</a
            >
          </p>
          <p>
            <a @click="view(record, 4)">{{ record.userState === 1 ? '启用' : '禁用' }}账号</a>
          </p>
          <p>
            <a @click="view(record, 5)">重置实名信息</a>
          </p>
        </div>
      </s-table>
      <a-modal v-model="visible" title="用户详情" class="modal">
        <template #footer>
          <a-button type="primary" @click="visible = false"> 关闭 </a-button>
        </template>
        <div class="info_style">
          <p>用户基础信息</p>
          <div class="user_basic_info">
            <div class="user_harder_img">
              <span>头像：</span>
              <img :src="require('@/assets/images/header.png')" alt="" />
            </div>
            <div class="user_info">
              <div class="item">
                <div>
                  <span>用户ID：</span><span>{{ info?.UserBasicInfo?.userNo }}</span>
                </div>
                <div>
                  <span>登录名：</span><span>{{ info?.UserBasicInfo?.userPhone }}</span>
                </div>
                <div>
                  <span>用户状态：</span><span>{{ option1[info?.UserBasicInfo?.userState]?.label }}</span>
                </div>
                <div>
                  <span>用户昵称：</span><span>{{ info?.UserBasicInfo?.nickName }}</span>
                </div>
                <div>
                  <span>邀请人ID：</span><span>{{ info?.UserBasicInfo?.inviteUserNo }}</span>
                </div>
                <div>
                  <span>创建时间：</span><span>{{ info?.UserBasicInfo?.createTime }}</span>
                </div>
                <div>
                  <span>真实姓名：</span><span>{{ info?.UserBasicInfo?.realName || '-' }}</span>
                </div>
                <div>
                  <span>身份证：</span><span>{{ info?.UserBasicInfo?.idNumber || '-' }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="info_style">
          <p>用户收款信息</p>
          <div v-if="info?.UserPaymentInfoList?.length > 0" class="back_info">
            <div v-for="bankItem in info?.UserPaymentInfoList" class="back_info_item">
              <div class="item">
                <div>
                  <span>银行名称：</span><span>{{ bankItem?.bankName }}</span>
                </div>
                <div>
                  <span>真实名称：</span><span>{{ bankItem?.username }}</span>
                </div>
                <div>
                  <span>银行卡号：</span><span>{{ bankItem?.bankNumber }}</span>
                </div>
              </div>
            </div>
          </div>
          <div v-else class="no_data">
            <a-empty description="未绑定收款信息" />
          </div>

          <a-button
            :disabled="!info?.UserPaymentInfoList?.length > 0"
            :type="!info?.UserPaymentInfoList?.length > 0 ? 'dashed' : 'primary'"
            class="reset_bank_btn"
            @click="resetBankInfo"
          >重置所有银行卡信息
          </a-button>
        </div>
        <div class="info_style">
          <p>用户资金信息</p>
          <div class="user_info">
            <div class="item">
              <div>
                <span>资产余额：</span><span>{{ common.numFormat(info?.UserFundsInfo?.fundsAmount ?? 0) }}</span>
              </div>
              <div>
                <span>余额：</span><span>{{ common.numFormat(info?.UserFundsInfo?.walletBalance ?? 0) }}</span>
              </div>
              <div>
                <span>积分：</span><span>{{ common.numFormat(info?.UserFundsInfo?.integral ?? 0) }}</span>
              </div>
              <div>
              <span>投资项目：</span
              ><span>{{ common.numFormat(info?.UserFundsInfo?.investmentProjectAmount ?? 0) }}</span>
              </div>
              <div>
                <span>充值金额：</span><span>{{ common.numFormat(info?.UserFundsInfo?.rechargeAmount ?? 0) }}</span>
              </div>
              <div>
                <span>提现金额：</span><span>{{ common.numFormat(info?.UserFundsInfo?.withdrawAmount ?? 0) }}</span>
              </div>
            </div>
          </div>
        </div>
      </a-modal>
      <a-modal title="提示信息" v-model="visible1" @ok="handleOk(0)">
        <p>确定要重置密码？</p>
        <p>注：[用户ID：{{ info?.userNo }}]将重置{{ `${passwordType === 'pay' ? '支付' : '登录'}` }}密码</p>
      </a-modal>

      <a-modal title="提示信息" v-model="visible2" @ok="handleOk(1)">
        <p>确定要{{ `${info.userState === 2 ? '解冻' : '冻结'}` }}账号？</p>
        <p>
          注：[用户ID：{{ info?.userNo }}] {{ `${info.userState === 2 ? '将取消提现限制' : ' 将冻结账号，不可提现'}` }}
        </p>
      </a-modal>
      <a-modal title="提示信息" v-model="visible3" @ok="handleOk(2)">
        <p>确定要{{ `${info.userState === 1 ? '启用' : '禁用'}` }}账号？</p>
        <p>注：[用户ID：{{ info?.userNo }}]{{ `${info.userState === 1 ? '将正常登录APP' : '将无法登录APP'}` }}</p>
      </a-modal>
      <a-modal title="提示信息" v-model="visible4" @ok="handleOk(3)">
        <p>确定要重置实名信息？</p>
        <p>注：[用户ID：{{ info?.userNo }}]将清除实名信息</p>
      </a-modal>
      <a-modal v-model="successModal" :footer="null" class="success_modal">
        <p>重置成功</p>
        <p class="password">密码: {{ newPassword }} <span class="copy" @click="copy">复制</span></p>
      </a-modal>
    </div>
  </div>
</template>

<script>
import STable from '@/components/Table'
import {
  getUserList,
  userDetail,
  userResetBankInfo,
  userResetPassword,
  userResetCertification,
  userDisableAccount,
  userFreezeAccount,
} from '@/api/user-manage'
import { mapGetters } from 'vuex'
import moment from 'moment'
import { common, copyTextToClipboard } from '@/common/index'
export default {
  name: 'userList',
  components: {
    STable,
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  data() {
    return {
      form: this.$form.createForm(this),
      visible: false,
      visible1: false,
      visible2: false,
      visible3: false,
      visible4: false,
      successModal: false,
      newPassword: '',
      passwordType: '',
      info: {},
      option: [
        { label: '未实名', value: 0, key: 0 },
        { label: '已实名', value: 3, key: 3 },
      ],
      option1: [
        { label: '正常', value: 0, key: 0 },
        { label: '禁用', value: 1, key: 1 },
        { label: '冻结', value: 2, key: 2 },
      ],
      columns: [
        {
          title: '创建时间',
          dataIndex: 'createTime',
          align: 'center'
        },
        {
          title: '用户ID',
          dataIndex: 'userNo',
          align: 'center'
        },
        {
          title: '登录名',
          dataIndex: 'userPhone',
          align: 'center'
        },
        {
          title: '用户昵称',
          dataIndex: 'nickName',
          align: 'center'
        },
        {
          title: '用户头像',
          dataIndex: 'avatar',
          scopedSlots: { customRender: 'avatar' },
          align: 'center'
        },
        {
          title: '邀请人ID',
          dataIndex: 'inviteUserNo',
          align: 'center'
        },
        {
          title: '状态',
          dataIndex: 'userState',
          customRender: (text) => this.option1[text]?.label,
          align: 'center'
        },
        {
          title: '实名',
          dataIndex: 'certificationState',
          customRender: (text) => (text === 3 ? '已实名' : '未实名'),
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          align: 'center'
        },
      ],
      // 查询条件参数
      queryParam: { time: [null, null], userNo: '', userPhone: '', certificationState: '', userState: '' },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const { userId } = this.userInfo

        this.queryParam.review = this.queryParam.review !== '' ? +this.queryParam.review : ''
        let params = {
          ...parameter,
          userId,
          ...this.queryParam,
          startTime: this.queryParam.time[0] ? `${this.queryParam.time[0]} 00:00:00` : '',
          endTime: this.queryParam.time[1] ? `${this.queryParam.time[1]} 23:59:59` : '',
        }
        delete params.time
        return new Promise((resolve, reject) => {
          getUserList(params)
            .then((res) => {
              if (res.code === 200) {
                resolve(res.data)
              } else {
                this.$message.error(res?.msg)
              }
            })
            .catch((err) => {
              reject(err)
            })
            .finally(() => {})
        })
      },
    }
  },
  methods: {
    moment,
    onChangeData(dates, dateStrings) {
      this.queryParam.time = dateStrings
    },
    disabled(current) {
      return current && current > moment().subtract().endOf('day')
    },
    view(row, type) {
      if (type !== 0) {
        this.info = row
        console.log(row, 'row')
      }
      switch (type) {
        case 0:
          this.visible = true
          const { userId } = this.userInfo
          let params = {
            userNo: row.userNo,
            userId,
          }
          userDetail(params).then((res) => {
            if (res.code === 200) {
              console.log(res.data, 'res.data')
              this.info = res.data ?? {}
            }
          })
          break
        case 1:
          this.visible1 = true
          this.passwordType = 'login'
          break
        case 2:
          this.visible1 = true
          this.passwordType = 'pay'
          break
        case 3:
          this.visible2 = true
          break
        case 4:
          this.visible3 = true
          break
        case 5:
          this.visible4 = true
          break
      }
    },
    refresh(isBackToFirstPage = false) {
      this.$refs.table.refresh(isBackToFirstPage)
    },
    onchange(data) {
      this.queryParam.review = data.key
    },
    search() {
      this.refresh(false)
    },
    searchReset() {
      this.queryParam = { time: [null, null], userNo: '', userPhone: '', certificationState: '', userState: '' }
      this.refresh(true)
    },
    // 清空银行卡信息
    resetBankInfo() {
      const { userId } = this.userInfo
      const params = {
        userId,
        userNo: this.info.UserBasicInfo.userNo,
        walletId: this.info.UserBasicInfo.walletId,
      }
      userResetBankInfo(params).then((res) => {
        if (res.code === 200) {
          this.info.UserPaymentInfoList = []
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleOk(type) {
      const { userId } = this.userInfo
      const obj = {
        userNo: this.info.userNo,
        userId,
      }
      let params = {}
      let url
      let title = ''
      let content = ''
      switch (type) {
        case 0:
          params = {
            ...obj,
            passwordType: this.passwordType === 'pay' ? 1 : 0,
          }
          title = '重置成功'
          url = userResetPassword
          break
        case 1:
          params = {
            ...obj,
            operationType: this.info.userState === 2 ? 1 : 0, // 0-冻结 1-解冻
          }
          title = `${this.info.userState === 2 ? '解冻' : '冻结'}成功`
          url = userFreezeAccount
          break
        case 2:
          params = {
            ...obj,
            operationType: this.info.userState === 1 ? 1 : 0, // 0-禁用 1-启用
          }
          title = `${this.info.userState === 2 ? '启用' : '禁用'}成功`
          url = userDisableAccount
          break
        case 3:
          params = {
            ...obj,
          }
          title = '重置成功'
          url = userResetCertification
          break
      }

      url(params).then((res) => {
        if (res.code === 200) {
          this.$message.success(res.msg)
          if (type === 0) {
            this.visible1 = false
            this.newPassword = res.data.newPassword
            this.successModal = true
          } else {
            content = `[用户ID = ${this.info.userNo}]`
            this.visible2 = false
            this.visible3 = false
            this.visible4 = false
          }
          if (type !== 0) {
            this.$success({
              title,
              content: <div style="color: #999999;">{content}</div>,
            })
          }
          this.$refs.table.refresh()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    copy() {
      copyTextToClipboard(this.newPassword)
      console.log(this.newPassword, '22222')
    },
  },
}
</script>
<style lang="less" scoped>
.user_list {
  .form {
    display: flex;
    margin-bottom: 20px;
    flex-wrap: wrap;
  }
  .avatar {
    width: 50px;
    height: 50px;
    border-radius: 50%;
  }
}
.modal {
  /deep/ .ant-modal {
    width: 1200px !important;
  }
  .info_style {
    border: 1px solid #eeeeee;
    border-radius: 5px;
    padding: 10px;
    margin-bottom: 20px;
    p {
      color: #299be0;
      font-size: 20px;
    }
    .user_basic_info {
      display: flex;
    }
    .user_info {
      width: 85%;
      display: flex;
      .item {
        display: flex;
        flex-wrap: wrap;
        div {
          display: flex;
          margin-left: 30px;
          min-width: 210px;
        }
      }
    }
    .user_harder_img {
      display: flex;
      align-items: center;
      margin-bottom: 20px;
      width: 10%;

      img {
        width: 30px;
        height: 30px;
        border-radius: 50%;
      }
    }
    .no_data {
      text-align: center;
    }
  }
  .back_info {
    display: flex;
    flex-wrap: wrap;
    .back_info_item {
      .item {
        margin-right: 25px;
        margin-bottom: 10px;
        border: 1px solid #eeeeee;
        border-radius: 5px;
        padding: 20px;
      }
    }
  }
  .reset_bank_btn {
    margin: 10px 0;
  }
}
.success_modal {
  text-align: center;
  .password {
    color: #999;
    font-size: 12px;
  }
  .copy {
    color: #299be0;
    margin-left: 10px;
    cursor: pointer;
  }
}
</style>
